﻿using System;
using System.Collections.Generic;
using Expect.VisualStudio.Extensions;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Tagging;

namespace Expect.Vs2010.Extensions.Tagging
{    
    class FixtureTagger : ITagger<IFixtureTag>
    {
        private ITextBuffer _buffer;

        private IExpectVSService _expectService;

        public FixtureTagger(ITextBuffer buffer)
        {
            _buffer = buffer;
            _expectService = (IExpectVSService)Package.GetGlobalService(typeof(SExpectVSService));
            
        }

        public IEnumerable<ITagSpan<IFixtureTag>> GetTags(Microsoft.VisualStudio.Text.NormalizedSnapshotSpanCollection spans)
        {
            if (spans.Count == 0)
                yield break;

            var item = _expectService.GetCurrentItemResolution();
            
            if (item == null) yield break;

            foreach (SnapshotSpan elt in spans)
            {
                var text = elt.GetText();
                var trimmed = text.Trim();
                var line = elt.Start.GetContainingLine();
                
                if (!string.IsNullOrEmpty(trimmed))
                {
                    var resolution = _expectService.GetResolutionForLine(line.LineNumber);
                    if (resolution != null)
                        yield return new TagSpan<IFixtureTag>(elt, new FixtureTag(elt, resolution));
                }
            }
        }

        public event EventHandler<Microsoft.VisualStudio.Text.SnapshotSpanEventArgs> TagsChanged;
    }
}
